package encheres.presentation.ui;

import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.Rectangle;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.omg.CORBA.ORB;
import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContext;
import org.omg.CosNaming.NamingContextHelper;
import org.omg.CosNaming.NamingContextPackage.CannotProceed;
import org.omg.CosNaming.NamingContextPackage.NotFound;

import encheres.exceptions.EncheresCorbaException;
import encheres.exceptions.EncheresUIException;
import encheres.manager.corba.I_ClientHelper;
import encheres.manager.corba.I_ClientOperations;
import encheres.presentation.controleur.CreationCompte;

public class JPanelCreationCompte extends JPanel {

    private JLabel labelName = null;
    private JTextField nameTextField = null;
    private JLabel labelAdress = null;
    private JTextField adressTextField = null;
    private JLabel labelLogin = null;
    private JTextField loginTextField = null;
    private JLabel labelPasswordValue = null;
    private JButton buttonSave = null;
    private JLabel labelMessageValidation = null;
	private JPanel jPanelCoordonnees = null;
	private JPanel jPanelPassword = null;
	private JLabel jLabel = null;

    public JPanelCreationCompte() throws HeadlessException {
        super();
        initialize();
    }

    /**
     * This method initializes this
     * 
     */
    private void initialize() {
    	
    	jLabel = new JLabel();
    	jLabel.setBounds(new Rectangle(30, 20, 262, 30));
    	jLabel.setText("Ajouter un nouvel utilisateur");
    	jLabel.setFont(new Font("Dialog", Font.BOLD, 18));
        this.setLayout(null);
        
        // le nom
        labelName = new JLabel();
        labelName.setText("Nom : ");
        labelName.setBounds(new Rectangle(30, 30, 35, 16));
        
        
        // l'adresse
        labelAdress = new JLabel();
        labelAdress.setText("Adresse : ");
        labelAdress.setBounds(new Rectangle(30, 60, 57, 16));
        
        
        // le login
        labelLogin = new JLabel();
        labelLogin.setText("Login : ");
        labelLogin.setBounds(new Rectangle(30, 90, 40, 16));
        
        
        // le mot de passe
        
        labelPasswordValue = new JLabel();
        labelPasswordValue.setText("Après la création de cet utilisateur, vous recevrez son mot de passe.");
        labelPasswordValue.setBounds(new Rectangle(34, 36, 396, 16));
        
        // le bouton enregistrer
        
        // le message de validation
        labelMessageValidation = new JLabel();
        labelMessageValidation.setBounds(new Rectangle(0, 0, 0, 0));
        this.add(getButtonSave(), null);
        this.add(labelMessageValidation, null);
        this.add(getJPanelCoordonnees(), null);
        this.add(getJPanelPassword(), null);
        this.add(jLabel, null);
    	
        this.passerEnModeCreation(true);
		
    }

    /**
     * 
     * @param modeCreation
     */
    private void passerEnModeCreation(final boolean modeCreation) {
        this.labelMessageValidation.setText("L'utilisateur "+this.nameTextField.getText()+" a été ajouté avec success.");
        this.labelMessageValidation.setVisible(!modeCreation);
        
        this.loginTextField.setEnabled(modeCreation);
        this.nameTextField.setEnabled(modeCreation);
        this.adressTextField.setEnabled(modeCreation);
    }

    /**
     * This method initializes nameTextField
     * 
     * @return javax.swing.JTextField
     */
    private JTextField getNameTextField() {
        if (nameTextField == null) {
            nameTextField = new JTextField();
            nameTextField.setColumns(30);
            nameTextField.setBounds(new Rectangle(90, 29, 335, 20));
        }
        return nameTextField;
    }

    /**
     * This method initializes adressTextField
     * 
     * @return javax.swing.JTextField
     */
    private JTextField getAdressTextField() {
        if (adressTextField == null) {
            adressTextField = new JTextField();
            adressTextField.setColumns(30);
            adressTextField.setBounds(new Rectangle(90, 59, 335, 20));
        }
        return adressTextField;
    }

    /**
     * This method initializes loginTextField
     * 
     * @return javax.swing.JTextField
     */
    private JTextField getLoginTextField() {
        if (loginTextField == null) {
            loginTextField = new JTextField();
            loginTextField.setColumns(30);
            loginTextField.setBounds(new Rectangle(90, 89, 335, 20));
        }
        return loginTextField;
    }

    /**
     * This method initializes buttonSave
     * 
     * @return javax.swing.JButton
     */
    private JButton getButtonSave() {
        if (buttonSave == null) {
            buttonSave = new JButton();
            buttonSave.setText("Enregistrer");
            buttonSave.setBounds(new Rectangle(383, 306, 98, 26));
            buttonSave.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                	try {
                		// Pré-condition
                		if(	getLoginTextField().getText().isEmpty() ||
                				getNameTextField().getText().isEmpty()) {
                			JOptionPane.showMessageDialog(buttonSave.getRootPane(), "Une des caractéristiques du nouvel utilisateur n'est pas rempli !", "Encheres", JOptionPane.WARNING_MESSAGE);
                		} else {

                			CreationCompte mgrCompte = new CreationCompte();
	                		// Récupération
	                		String login = getLoginTextField().getText();
	                		labelPasswordValue.setText(mgrCompte.generatePassword(login));
	                		String password = labelPasswordValue.getText();
	                		String nom = getNameTextField().getText();
	                		String adresse = getAdressTextField().getText();

	                		int resultat = CorbaManager.getManagerClient().creerCompte(login, password, nom, adresse);
	                		
	                		switch (resultat) {
							case 0:
								throw new EncheresUIException();
							case -1:
								throw new EncheresCorbaException();
							default:
								getButtonSave().setEnabled(false);
								passerEnModeCreation(false);
								break;
							}
                		}
                	} catch (EncheresUIException uiException) {
                		passerEnModeCreation(true);
                		JOptionPane.showMessageDialog(buttonSave.getRootPane(), "Erreur : le nouvel utilisateur n'a pas été créé !", "Encheres", JOptionPane.ERROR_MESSAGE);
					} catch (EncheresCorbaException corbaException){
						passerEnModeCreation(true);
						JOptionPane.showMessageDialog(buttonSave.getRootPane(), "Une erreur inconnue est arrivée !\n\n"+corbaException.getMessage(), "Encheres", JOptionPane.ERROR);
					} catch (Exception exception) {
						passerEnModeCreation(true);
						JOptionPane.showMessageDialog(buttonSave.getRootPane(), "Une erreur inconnue est arrivée !\n\n"+exception.getMessage(), "Encheres", JOptionPane.ERROR);
					}
                    
                }

				
            });
        }
        return buttonSave;
    }

	/**
	 * This method initializes jPanelCoordonnees	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelCoordonnees() {
		if (jPanelCoordonnees == null) {
			jPanelCoordonnees = new JPanel();
			jPanelCoordonnees.setLayout(null);
			jPanelCoordonnees.setBounds(new Rectangle(30, 65, 452, 128));
			jPanelCoordonnees.setBorder(BorderFactory.createTitledBorder("Coordonnées"));
			jPanelCoordonnees.add(labelName, null);
			jPanelCoordonnees.add(labelAdress, null);
			jPanelCoordonnees.add(labelLogin, null);
			jPanelCoordonnees.add(getNameTextField(), null);
			jPanelCoordonnees.add(getAdressTextField(), null);
			jPanelCoordonnees.add(getLoginTextField(), null);
		}
		return jPanelCoordonnees;
	}

	/**
	 * This method initializes jPanelPassword	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelPassword() {
		if (jPanelPassword == null) {
			jPanelPassword = new JPanel();
			jPanelPassword.setLayout(null);
			jPanelPassword.setBounds(new Rectangle(30, 202, 452, 81));
			jPanelPassword.setBorder(BorderFactory.createTitledBorder("Mot de passe"));
			jPanelPassword.add(labelPasswordValue, null);
		}
		return jPanelPassword;
	}
}
